import { AxiosRequestConfig, AxiosResponse } from 'axios'
// import { notification } from 'ant-design-vue'
import loading from './loading'

// 错误响应通知提醒框
function respNotification(errorInfo: any) {
  // notification.error({
  //   message: '',
  //   description: () => h('div', errorInfo),
  // })
}

export default {
  request: {
    onFulfilled(request: AxiosRequestConfig) {
      /**
       * 根据项目实际情况来对 config 做处理
       * 这里对 config 不做任何处理，直接返回
       */
      // const { token } = Store.getters
      
      request.headers = {
        authorization: 'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzaWduX2RhdGUiOjE2ODE5NzgxMjYsInVzZXJfaWQiOjh9.iEUyWUihfNtmattQ3_ZB6ABSwJfKR-lqB9GvZeu35GI'
      }
      // if (token) {
      //   request.headers.Authorization = `${token}`
      // }
      if ((<any>request).loading) {
        loading.start()
      }
      return request
    },
    onRejected(error: any): Promise<never> {
      return Promise.reject(error)
    },
  },
  // 响应拦截器
  response: {
    onFulfilled(response: AxiosResponse) {
      const { headers } = response
      const contentDes = headers['content-disposition']
      if ((<any>response).loading) {
        loading.end()
      }
      if (contentDes) {
        return response
      }
      return response.data
    },
    onRejected(error: any) {
      const { code, message, response } = error
      console.log('http:error', response)
      if (response && response.data) {
        const respData = response.data
        respNotification(respData)
        // 状态码为 -1200X的时候为Token失效 需要实现登出
        const respDataCode = respData.code
        if (respDataCode > -12999 && respDataCode < -12000) {
          // Store.dispatch('user/loginOut')
        }
      } else if (message) {
        // ! 暂时这样
        // if (import.meta.env.VITE_ENV !== 'test') {
        // notification.error({
        //   description: message,
        //   message: code,
        // })
        // }
      }
      return Promise.reject(error)
    },
  },
}
